package com.ssmtest.service.serviceimpl;

import com.ssmtest.dao.StudentDao;
import com.ssmtest.entity.PageBean;
import com.ssmtest.entity.Student;
import com.ssmtest.entity.Subject;
import com.ssmtest.entity.User;
import com.ssmtest.service.StudentService;
import com.ssmtest.util.Page;
import com.ssmtest.util.PageResult;
import com.ssmtest.util.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service("studentService")
public class StudentServiceImpl implements StudentService {
	@Resource
	private StudentDao studentdao;

	/**
	 * 判断学生是否符合登录条件
	 */
	@Override
	public Student allowLogin(String studentID, String password){
      return studentdao.allowLogin(studentID,password);
	}
	/**
	 * 根据学生的班级返回结果
	 */



	/**
	 * 根据学生姓名返回结果
	 */
	@Override
	public List<Student> getStudentByName(String studentName) {
		return studentdao.findByStudentName(studentName);
	}

	@Override
	public PageResult getStudentByClass(String sclass, Page page) {
		return null;
	}

	/**
	 * 根据id返回
	 */
	@Override
	public Student getStudentInfo(String studentID) {
		return studentdao.findByStudenID(studentID);
	}
	/**
	 * 根据学生id更新学生成绩
	 */
	@Override
	public void setStudentResult(String studentID, int result) {
		Student student = studentdao.findByStudenID(studentID);
		student.setResult(result);
		studentdao.updateStudent(student);
	}
	/**
	 * 保存一条学生记录
	 */
	@Override
	public void saveStudent(Student student) {
		studentdao.save(student);
	}
	/**
	 * 根据分页  查找学生
	 */
	@Override
	public PageResult queryStudentByPage(Page page) {
		page = PageUtil.createPage(page.getEveryPage(), page.getTotalCount(), page.getCurrentPage());
		List<Student> list = studentdao.findStudentsByPage(page);
		PageResult result = new PageResult(page, list);
		return result;
	}
	/**
	 * 获取学生数量
	 */
	@Override
	public int countStudent() {
		return studentdao.fintStudentCount();
	}
	/**
	 * 返回班级学生数量
	 */
	@Override
	public int countStudentByClass(String sclass) {
		return studentdao.fintStudentCountByClass(sclass);
	}

	@Override
	public int insertSelective(String studentID,String studentName, String sclass) {
		return studentdao.insertSelective(studentID,studentName,sclass);
	}

	@Override
	public int updateByPrimaryKeySelective(String studentID,String studentName, String sclass) {
		return studentdao.updateByPrimaryKeySelective(studentID,studentName,sclass);
	}
	public PageBean<Student> findByPage(int currentPage) {

		PageBean<Student> pageBean = new PageBean<Student>();

		//封装当前页数
		pageBean.setCurrPage(currentPage);

		//每页显示的数据
		int pageSize=5;
		pageBean.setPageSize(pageSize);

		//封装总记录数
		int totalCount = studentdao.selectCount();
		pageBean.setTotalCount(totalCount);

		//封装总页数
		double tc = totalCount;
		Double num =Math.ceil(tc/pageSize);//向上取整
		pageBean.setTotalPage(num.intValue());
		int start = (currentPage-1)*pageSize;
		int size=pageBean.getPageSize();
		//map.put("start",(currentPage-1)*pageSize);
		//map.put("size", pageBean.getPageSize());
		//封装每页显示的数据
		List<Student> lists = studentdao.findByPage(start,size);
		System.out.println(studentdao.findByPage(start,size));
		pageBean.setLists(lists);

		return pageBean;
	}

	@Override
	public List<Student> selectbyclass(String sclass) {
		return studentdao.selectbyclass(sclass);
	}

	@Override
	public List<Student> selectbyname(String studentName) {
		return studentdao.selectbyName(studentName);
	}

	@Override
	public List<Student> selectbyid(String studentID) {
		return studentdao.selectbyid(studentID);
	}

	@Override
	public int insertxuanzesubject(String stTitle, String stOptionA, String stOptionB, String stOptionC, String stOptionD, String stAnswer, String stParse) {
		return studentdao.insertxuanzesubject(stTitle,stOptionA,stOptionB,stOptionC,stOptionD,stAnswer,stParse);
	}

	@Override
	public int inserttiankongsubject(String stTitle, String stAnswer, String stParse) {
		return studentdao.inserttiankongsubject(stTitle,stAnswer,stParse);
	}

	@Override
	public int insertpanduansubject(String stTitle, String stAnswer, String stParse) {
		return studentdao.insertpanduansubject(stTitle,stAnswer,stParse);
	}

	@Override
	public int insertwendasubject(String stTitle, String stAnswer, String stParse) {
		return studentdao.insertwendasubject(stTitle,stAnswer,stParse);
	}

	@Override
	public PageBean<Subject> findByPagexuanze(int currentPage) {
		PageBean<Subject> pageBean = new PageBean<Subject>();

		//封装当前页数
		pageBean.setCurrPage(currentPage);

		//每页显示的数据
		int pageSize=5;
		pageBean.setPageSize(pageSize);

		//封装总记录数
		int totalCount = studentdao.selectxuanzecount();
		pageBean.setTotalCount(totalCount);

		//封装总页数
		double tc = totalCount;
		Double num =Math.ceil(tc/pageSize);//向上取整
		pageBean.setTotalPage(num.intValue());
		int start = (currentPage-1)*pageSize;
		int size=pageBean.getPageSize();

		//封装每页显示的数据
		List<Subject> lists = studentdao.findByPagexuanze(start,size);
		System.out.println(studentdao.findByPagexuanze(start,size));
		pageBean.setLists(lists);
		return pageBean;
	}

}
